//
//  ViewController.swift
//  Test
//
//  Created by alyadan on 2017/3/8.
//  Copyright © 2017年 alyadan. All rights reserved.
//

import UIKit
import YD_DataModel
import ObjectMapper
import SwiftyJSON
import Moya
import RxSwift


class User: Mappable {
    required init?(map: Map) {
    }
    
    var id: String?
    
    func mapping(map: Map) {
        id <- map["id"]
    }
    
    
}

class ViewController: UIViewController {

    let disposeBag = DisposeBag()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        YDNetworkTool.responseDeal = ResponseHandlerExampleJustData()
        let phone = "13968933306"
        _ = YDNetworkTool.requestRx(target: TimeManagerApi.sms(phone: phone, type: .register))
            .subscribe { x in
                print(x)
            } onError: { err in
                print(err)
            }
        
        /*
//        _ = YDNetworkTool.requestRx(target: TimeManager.login(phone: "13968933300", password: "123456"))
        _ = User.fetchModel(target: TimeManagerApi.user, dataKeyPath: "userInfo")
            .subscribe { x in
                print(x)
            } onError: { err in
                
            }

        
        _ = YDNetworkTool.requestRx(target: TimeManagerApi.user)
            .map{User.genModel(data: $0, dataKeyPath: "userInfo")}
            .subscribe { x in
                print(x)
            } onError: { err in
              print(err)
            }
         */

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

enum SmsType: String, Codable {
    case register, forget
}

class UserDataModel {
    static var token: String? = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiJGOTg2MTM3Ni04N0VELTREMTYtOTA4MC0zMEJEMEIyQjBGNDAiLCJleHBpcmF0aW9uVGltZSI6ODY0MDAwLCJleHBpcmF0aW9uIjoxNjM5Mjk3MTk4LjE1Mzk0NH0.AUc2BlF20lG3qKnTFlrh9oT5PGvBVvqxTGRkUh0MGbQ"
}

enum TimeManagerApi: TargetType {
    
    var headers: [String : String]? {
        return ["Authorization": "Bearer " + (UserDataModel.token ?? "")]
    }
    
    case login(phone: String, password: String )
    case register(phone: String, password: String )
    case user
    case sms(phone: String, type: SmsType)
    
    static let baseURLStr = "http://127.0.0.1:8080/api"
    var baseURL: URL { return try! TimeManagerApi.baseURLStr.asURL() }
    
    var path: String {
        switch self {
        case .sms:
            return "/sms"
        case .login:
            return "/login"
        case .register:
            return "/register"
        case .user:
            return "/user"
        }
    }
    
    var method: Moya.Method {
        switch self {
        case .register, .login, .sms:
            return .post
        default:
            return .get
        }
    }
    
    var task: Task {
        var param: [String: Any] = [:]
        switch self {
        case .sms(phone: let phone, type: let type):
            param = ["phone": phone, "type": type.rawValue]
        case .login(phone: let phone, password: let password):
            param = ["phone": phone, "password": password]
        case .register(phone: let phone, password: let password):
            param = ["phone": phone, "password": password]
        default:
            return .requestPlain
        }
        
        switch self.method {
        case .get:
            return .requestParameters(parameters: param, encoding: URLEncoding.default)
        default:
            return .requestParameters(parameters: param, encoding: JSONEncoding.default)
        }
    }
}


